package org.happycomp.radiog.dialog.plan;

import java.util.Calendar;
import java.util.Date;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.happycomp.radio.Station;
import org.happycomp.radiog.dialog.common.DateDialog;

public class PlanNewDialog extends Dialog {

	private int startTimeMinutes;
	private int startTimeHours;

	private int stopTimeMinutes;
	private int stopTimeHours;

	private int day;
	private int month;
	private int year;
	
	private Station station = Station.CRO1;
	private String title;
	private boolean shutdown = false;
	
	public PlanNewDialog(IShellProvider parentShell) {
		super(parentShell);
		initDate(new Date());
	}

	public PlanNewDialog(Shell parentShell) {
		super(parentShell);
		initDate(new Date());
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button okButton = createButton(parent, IDialogConstants.OK_ID,
				IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);

		okButton.setEnabled(false);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);

		GridData gdata = new GridData();
		gdata.grabExcessHorizontalSpace = false;
		gdata.horizontalAlignment = SWT.LEFT;
		CLabel label = new CLabel(container, SWT.NONE);
		label.setText("Jmeno planovni:");
		label.setLayoutData(gdata);

		gdata = new GridData();
		gdata.grabExcessHorizontalSpace = true;
		gdata.horizontalAlignment = SWT.FILL;
		gdata.horizontalSpan = 2;
		gdata.minimumHeight = 100;
		gdata.minimumWidth = 300;
		final Text nameText = new Text(container, SWT.BORDER);
		nameText.addKeyListener(new KeyListener() {
			
			@Override
			public void keyReleased(KeyEvent e) {
				String text = nameText.getText();
				if ((text != null) && (!text.equals(""))) {
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				} else {
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				title = text;
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		nameText.setLayoutData(gdata);

		gdata = new GridData();
		gdata.grabExcessHorizontalSpace = false;
		gdata.horizontalAlignment = SWT.LEFT;
		label = new CLabel(container, SWT.NONE);
		label.setText("Stanice:");
		label.setLayoutData(gdata);

		gdata = new GridData();
		gdata.grabExcessHorizontalSpace = true;
		gdata.horizontalAlignment = SWT.FILL;
		final Combo combo = new Combo(container, SWT.READ_ONLY);
		String[] stationNames = new String[Station.values().length];
		for (int i = 0; i < stationNames.length; i++) { stationNames[i] = Station.values()[i].name(); }
		combo.setItems(stationNames);
		combo.setLayoutData(gdata);
		combo.addSelectionListener(new SelectionListener() {
			

			@Override
			public void widgetSelected(SelectionEvent e) {
				station = Station.values()[combo.getSelectionIndex()];
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		combo.select(station.ordinal());
		
		gdata = new GridData();
		gdata.grabExcessHorizontalSpace = false;
		gdata.horizontalAlignment = SWT.LEFT;
		label = new CLabel(container, SWT.NONE);
		label.setText("Zacatek nahravani:");
		label.setLayoutData(gdata);

		gdata = new GridData();
		final DateTime startTime = new DateTime (container, SWT.TIME | SWT.SHORT | SWT.BORDER);
		gdata.grabExcessHorizontalSpace = true;
		gdata.horizontalAlignment = GridData.FILL;
		startTime.setLayoutData(gdata);
		startTime.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				startTimeMinutes = startTime.getMinutes();
				startTimeHours = startTime.getHours();
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
			}
		});

		gdata = new GridData();
		gdata.grabExcessHorizontalSpace = false;
		gdata.horizontalAlignment = SWT.LEFT;
		label = new CLabel(container, SWT.NONE);
		label.setText("Konec nahravani:");
		label.setLayoutData(gdata);

		gdata = new GridData();
		final DateTime stopTime = new DateTime (container, SWT.TIME | SWT.SHORT | SWT.BORDER);
		gdata.grabExcessHorizontalSpace = true;
		gdata.horizontalAlignment = GridData.FILL;
		stopTime.setLayoutData(gdata);
		stopTime.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				stopTimeMinutes = stopTime.getMinutes();
				stopTimeHours = stopTime.getHours();
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		gdata = new GridData();
		gdata.grabExcessHorizontalSpace = false;
		gdata.horizontalAlignment = SWT.LEFT;
		label = new CLabel(container, SWT.NONE);
		label.setText("Datum:");
		label.setLayoutData(gdata);

		createDateTextField(container);
		createShutdownAfterDownload(container);
		
		return container;
	}

	private void createShutdownAfterDownload(Composite container) {
		GridData gdata = new GridData();
		Composite composite = new Composite(container, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		
		gdata.grabExcessHorizontalSpace = true;
		gdata.horizontalAlignment = SWT.FILL;
		
		
		Button checkBtn = new Button(container, SWT.CHECK);
		checkBtn.setText(" Ulozit stav a vypnout ");
		checkBtn.setLayoutData(gdata);
		checkBtn.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				Button btn = (Button) e.getSource();
				shutdown = btn.getSelection();
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				Button btn = (Button) e.getSource();
				shutdown = btn.getSelection();
			}
		});
	}

	private void createDateTextField(Composite container) {
		GridData gdata = new GridData();
		Composite composite = new Composite(container, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		
		gdata.grabExcessHorizontalSpace = true;
		gdata.horizontalAlignment = SWT.FILL;
		composite.setLayoutData(gdata);
		
		gdata = new GridData();
		gdata.horizontalAlignment = GridData.FILL;
		gdata.grabExcessHorizontalSpace = true;
		gdata.minimumHeight = 13;
		final Text text = new Text(composite, SWT.BORDER);
		text.setEditable(false);
		text.setLayoutData(gdata);
		text.setText(DateDialog.SIMPLE_DATE_FORMATTER.format(new Date()));
		
		
		gdata = new GridData();
		final Button button = new Button(composite, SWT.PUSH);
		button.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				Button but = (Button) e.getSource();
				DateDialog dialog = new DateDialog(but.getShell());
				if (IDialogConstants.OK_ID == dialog.open()) {
					Date date = dialog.getDate();
					text.setText(DateDialog.SIMPLE_DATE_FORMATTER.format(date));
					initDate(date);
				}
			}

			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		button.setText("..");
		button.setLayoutData(gdata);
	}

	public Station getStation() {
		return this.station;
	}

	private void initDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		day = calendar.get(Calendar.DAY_OF_MONTH);
		month = calendar.get(Calendar.MONTH);
		year = calendar.get(Calendar.YEAR);
		
		this.startTimeHours = calendar.get(Calendar.HOUR_OF_DAY);
		this.startTimeMinutes= calendar.get(Calendar.MINUTE);

		this.stopTimeHours = calendar.get(Calendar.HOUR_OF_DAY);
		this.stopTimeMinutes= calendar.get(Calendar.MINUTE);
	}

	
	public Date getStartTime() {
		Calendar cal = Calendar.getInstance();
		
		cal.set(Calendar.DAY_OF_MONTH, this.day);
		cal.set(Calendar.YEAR, this.year);
		cal.set(Calendar.MONTH, this.month);
		
		cal.set(Calendar.MINUTE, this.startTimeMinutes);
		cal.set(Calendar.HOUR_OF_DAY, this.startTimeHours);

		return cal.getTime();
	}
	
	public  Date getStopTime() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, this.day);
		cal.set(Calendar.YEAR, this.year);
		cal.set(Calendar.MONTH, this.month);
		
		cal.set(Calendar.MINUTE, this.stopTimeMinutes);
		cal.set(Calendar.HOUR_OF_DAY, this.stopTimeHours);
		return cal.getTime();
		
	}
	
	public String getTitle() {
		return title;
	}
	
	
	public int getStartTimeHours() {
		return startTimeHours;
	}
	
	public int getStopTimeHours() {
		return stopTimeHours;
	}

	public int getStartTimeMinutes() {
		return startTimeMinutes;
	}
	public int getStopTimeMinutes() {
		return stopTimeMinutes;
	}
	
	public int getDay() {
		return day;
	}
	
	public int getMonth() {
		return month;
	}
	
	public int getYear() {
		return year;
	}

	public boolean isShutdown() {
		return shutdown;
	}

	public void setShutdown(boolean shutdown) {
		this.shutdown = shutdown;
	}
}
